//
//  MineViewController.m
//  XTXOneToOne
//
//  Created by Canny on 2018/10/29.
//  Copyright © 2018年 WK. All rights reserved.
//

#import "MineViewController.h"
#import "LoginViewController.h"
#import "MCMainNavigationController.h"
#import "MOLoadHttpsData.h"
#import "DLSetterViewController.h"
#import "RealNameAuthViewController.h"
#import "KKWebViewController.h"
#import "GGOrderListViewController.h"
#import "AccountViewController.h"
#import "MyMoneyViewController.h"
#import "NoticeCategoryViewController.h"
#import "FBOrderListViewController.h"

@interface MineViewController ()

@property (nonatomic,strong)UIImageView * imgV;
@property (nonatomic,strong)UILabel * imgVLab;

@property (nonatomic,strong)UILabel *label;
@property (nonatomic,strong)UIButton * btn;

@end

@implementation MineViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self setProperty];
    
    [self createUI];
    
    [self relayoutUI];

}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    LoginTool * LT = [LoginTool sharedLoginTool];

    if (LT.isGoToLogin) {
        LT.isGoToLogin=NO;
        MCMainNavigationController * nav = [[MCMainNavigationController alloc]initWithRootViewController:[[LoginViewController alloc] init]];
        
        [self presentViewController:nav animated:YES completion:nil];
    }
    [self relayoutUI];
    NSString * token = [LoginModel GetValue:DL_token];
    if (token.length>0) {
        [self loadData];
    }
    [UIApplication sharedApplication].statusBarHidden = NO;
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBarHidden=YES;
}

-(void)setProperty{
    
    self.view.backgroundColor=RGB(255, 255, 255);
    
    [UIApplication sharedApplication].statusBarHidden = NO;
    
   self.navigationController.navigationBar.translucent = YES;
   self.navigationController.navigationBarHidden=YES;
}


-(void)createUI{
    
    
    UIImageView * imgVArrow = [[UIImageView alloc]init];
    [self.view addSubview:imgVArrow];
    imgVArrow.backgroundColor=RGB(99, 139, 211);
    [imgVArrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(32);
        make.top.equalTo(self.view.mas_top).offset(80);
        make.width.equalTo(@(36));
        make.height.equalTo(@(36));
    }];
    imgVArrow.layer.cornerRadius=18;
    imgVArrow.image=[UIImage imageNamed:@"imgVArrow"];
    _imgV = imgVArrow;
    
    UILabel *imgVLab = [[UILabel alloc] init];
    imgVLab.text = @"";
    imgVLab.font = [UIFont boldSystemFontOfSize:18];
    imgVLab.textColor = RGB(255, 255, 255);
    imgVLab.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:imgVLab];
    [imgVLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(imgVArrow);
    }];
    _imgVLab = imgVLab;
    
    
    
    UIButton * btn = [[UIButton alloc]init];
    [self.view addSubview:btn];
    [btn setTitleColor:RGB(30, 63, 89) forState:UIControlStateNormal];
    [btn setTitle:@"请登录" forState:UIControlStateNormal];
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    btn.titleLabel.font = [UIFont boldSystemFontOfSize: 18.0];
    btn.tag = 3009;
    [btn addTarget:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imgVArrow.mas_right).offset(13);
        make.top.equalTo(imgVArrow.mas_top);
        make.width.equalTo(@(200));
        make.height.equalTo(@(20));
    }];
    _btn = btn;
    
    UILabel *label = [[UILabel alloc] init];
    label.text = @"ID 加载中...";
    label.font = [UIFont systemFontOfSize:12];
    label.textColor = RGB(131, 151, 167);
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(btn);
        make.top.equalTo(btn.mas_bottom).offset(10);
        make.width.equalTo(@(200));
        make.height.equalTo(@(15));
    }];
    _label = label;

    [self setBtnWithIndex:1 andImageName:@"我的资产" andlabelText:@"我的资产"];
    [self setBtnWithIndex:2 andImageName:@"订单管理" andlabelText:@"法币订单"];
    [self setBtnWithIndex:3 andImageName:@"订单管理" andlabelText:@"杠杆订单"];
    [self setBtnWithIndex:4 andImageName:@"收款方式" andlabelText:@"收款方式"];
    [self setBtnWithIndex:5 andImageName:@"实名认证" andlabelText:@"实名认证"];
    [self setBtnWithIndex:6 andImageName:@"账号设置" andlabelText:@"账号设置"];
    [self setBtnWithIndex:7 andImageName:@"公告中心" andlabelText:@"公告中心"];
    
}

-(void)setBtnWithIndex:(NSInteger)index andImageName:(NSString *)ImageName andlabelText:(NSString *)text{
    
    UIButton * btn = [[UIButton alloc]init];
    [self.view addSubview:btn];
    CGFloat X = 40,Y = 100+50*index,W = G_SCREENWIDTH-40,H = 50;
    btn.frame = CGRectMake(X, Y, W, H);
    btn.tag = 3000+index;
    [btn addTarget:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIImageView * imgV = [[UIImageView alloc]init];
    [btn addSubview:imgV];
//    imgV.backgroundColor=[UIColor cyanColor];
    [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(btn.mas_left).offset(0);
        make.centerY.equalTo(btn.mas_centerY);
        make.width.equalTo(@(18));
        make.height.equalTo(@(18));
    }];
    imgV.image=[UIImage imageNamed:ImageName];
    
    
    UILabel *label = [[UILabel alloc] init];
    label.text = text;
    label.font = [UIFont systemFontOfSize:15];
    label.textColor = RGB(30, 63, 89);
    [btn addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imgV.mas_right).offset(23);
        make.centerY.equalTo(btn.mas_centerY);
        make.width.equalTo(@(120));
        make.height.equalTo(@(30));
    }];
    
    
    UIImageView * imgVArrow = [[UIImageView alloc]init];
    [btn addSubview:imgVArrow];
//    imgVArrow.backgroundColor=[UIColor cyanColor];
    [imgVArrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(btn.mas_right).offset(-40);
        make.centerY.equalTo(btn.mas_centerY);
        make.width.equalTo(@(9));
        make.height.equalTo(@(18));
    }];
    imgVArrow.image=[UIImage imageNamed:@"进入icon"];

}


-(void)btnSelected:(UIButton *)btn{
    NSString * token = [LoginModel GetValue:DL_token];

    if (btn.tag==3001) {
//        [HUD showMessage:@"我的资产"];
        
        if (token.length>0) {
            
            
            MyMoneyViewController * VC = [[MyMoneyViewController alloc]init];
            [self.navigationController pushViewController:VC animated:YES];
            
        }else{
            MCMainNavigationController * nav = [[MCMainNavigationController alloc]initWithRootViewController:[[LoginViewController alloc] init]];
            [self presentViewController:nav animated:YES completion:nil];

        }
        
    }else if (btn.tag==3002){
//法币订单
        if (token.length>0) {
            FBOrderListViewController * VC = [[FBOrderListViewController alloc]init];
            [self.navigationController pushViewController:VC animated:YES];

        }else{
            MCMainNavigationController * nav = [[MCMainNavigationController alloc]initWithRootViewController:[[LoginViewController alloc] init]];
            [self presentViewController:nav animated:YES completion:nil];
            
        }
    }else if (btn.tag==3003){
//杠杆交易订单
        if (token.length>0) {
            GGOrderListViewController * VC = [[GGOrderListViewController alloc]init];
            [self.navigationController pushViewController:VC animated:YES];
            
        }else{
            MCMainNavigationController * nav = [[MCMainNavigationController alloc]initWithRootViewController:[[LoginViewController alloc] init]];
            [self presentViewController:nav animated:YES completion:nil];
            
        }
        
    }else if (btn.tag==3004){
        if (token.length>0) {
            AccountViewController * VC = [[AccountViewController alloc]init];
            [self.navigationController pushViewController:VC animated:YES];
        }else{
            MCMainNavigationController * nav = [[MCMainNavigationController alloc]initWithRootViewController:[[LoginViewController alloc] init]];
            [self presentViewController:nav animated:YES completion:nil];
            
        }
        
    }else if (btn.tag==3005){
        
        //        [HUD showMessage:@"实名认证"];
        if (token.length>0) {
            
            self.navigationController.navigationBarHidden = NO;
            RealNameAuthViewController * VC = [[RealNameAuthViewController alloc]init];
            [self.navigationController pushViewController:VC animated:YES];
        }else{
            MCMainNavigationController * nav = [[MCMainNavigationController alloc]initWithRootViewController:[[LoginViewController alloc] init]];
            [self presentViewController:nav animated:YES completion:nil];
            
        }
        
    }else if (btn.tag==3006){
        
        //        [HUD showMessage:@"账号设置"];
        if (token.length>0) {
            self.navigationController.navigationBarHidden = NO;
            DLSetterViewController * VC = [[DLSetterViewController alloc]init];
            [self.navigationController pushViewController:VC animated:YES];
            
        }else{
            MCMainNavigationController * nav = [[MCMainNavigationController alloc]initWithRootViewController:[[LoginViewController alloc] init]];
            [self presentViewController:nav animated:YES completion:nil];
        }
        

    }else if (btn.tag==3007){
        if (token.length>0) {
            
            NoticeCategoryViewController * VC = [[NoticeCategoryViewController alloc]init];
            [self.navigationController pushViewController:VC animated:YES];
            
        }else{
            
            MCMainNavigationController * nav = [[MCMainNavigationController alloc]initWithRootViewController:[[LoginViewController alloc] init]];
            [self presentViewController:nav animated:YES completion:nil];
            
        }
        
    }else if (btn.tag==3009){

        NSString * token = [LoginModel GetValue:DL_token];
        if (token.length>0) {
            return;
        }
        
        MCMainNavigationController * nav = [[MCMainNavigationController alloc]initWithRootViewController:[[LoginViewController alloc] init]];
        
        [self presentViewController:nav animated:YES completion:nil];

    }
}

-(void)loadData{
    
    __weak __typeof(self)wself = self;

    NSString * URL = [NSString stringWithFormat:@"%@user/profile",DEFAULTSEVERBASEURL];
    NSDictionary *Dic = @{};
    [MOLoadHttpsData GetHttpDataWithUrlStr:URL Dic:Dic SuccessBlock:^(id  _Nonnull responseObject) {
        
        NSDictionary * dic = responseObject;
        
        NSString * code = [NSString stringWithFormat:@"%@",dic[@"code"]];
        if ([code isEqualToString:@"0"]) {
            NSDictionary * dicc = dic[@"data"];
            if ([dicc isKindOfClass:[NSDictionary class]]) {
                
                MineUserModel * model  = [MineUserModel sharedMineUserModel];
                model = [MineUserModel mj_objectWithKeyValues:dicc];
                
                [wself relayoutUI];
                
            }
        }
        
    } FailureBlock:^(id  _Nonnull error) {
        
    }];
    
}

-(void)relayoutUI{
    
    NSString * token = [LoginModel GetValue:DL_token];
    if (token.length>0) {
        MineUserModel * model = [MineUserModel sharedMineUserModel];
        NSString * phone =[NSString stringWithFormat:@"%@",model.phone];
        if (phone.length>10) {
            _label.text = [NSString stringWithFormat:@"手机号 %@****%@",[phone substringToIndex:3],[phone substringFromIndex:7]];

        }else{
            _label.text = @"";

        }
        
        [_btn setTitle:model.nickname forState:UIControlStateNormal];
        
        _imgVLab.text = [model.nickname substringToIndex:1];
        
    }else{
        _label.text = @"";
        _imgVLab.text = @"♡";
        [_btn setTitle:@"请登录" forState:UIControlStateNormal];
    }

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
